package com.design.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.design.reggie.common.BaseContext;
import com.design.reggie.common.Result;
import com.design.reggie.dto.OrdersDto;
import com.design.reggie.entity.OrderDetail;
import com.design.reggie.entity.Orders;
import com.design.reggie.service.OrderDetailService;
import com.design.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders orders) {
        log.info("下单数据：{}", orders);
        ordersService.submit(orders);
        return Result.success("下单成功");
    }

    /**
     * 用户端订单明细
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public Result<Page<OrdersDto>> userPage(int page, int pageSize) {
        // 分页构造器
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        // 用户id要相等
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        wrapper.orderByDesc(ordersPage.getRecords()!= null, Orders::getOrderTime);
        ordersService.page(ordersPage, wrapper);
        Page<OrdersDto> ordersDtoPage = new Page<>();

        // 属性拷贝
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");

        List<Orders> ordersPageRecords = ordersPage.getRecords();
        List<OrdersDto> ordersDtoList = new ArrayList<>();
        for (Orders ordersPageRecord : ordersPageRecords) {
            OrdersDto ordersDto = new OrdersDto();
            // 首先拷贝records
            BeanUtils.copyProperties(ordersPageRecord, ordersDto);
            // 根据订单id查询订单明细
            LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OrderDetail::getOrderId, ordersPageRecord.getId());
            List<OrderDetail> list = orderDetailService.list(queryWrapper);
            ordersDto.setOrderDetails(list);
            ordersDtoList.add(ordersDto);

        }
        ordersDtoPage.setRecords(ordersDtoList);


        return Result.success(ordersDtoPage);
    }

    /**
     * 管理端查询订单明细
     * @param page
     * @param pageSize
     * @param number 订单号
     * @param beginTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Orders>> page(int page, int pageSize, String number, String beginTime, String endTime) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(number != null, Orders::getNumber, number);
        queryWrapper.between((beginTime != null && endTime != null), Orders::getOrderTime, beginTime, endTime);
        // ge结合le的方式等同于between
//        if (beginTime != null && endTime != null) {
//            queryWrapper.ge(Orders::getOrderTime, beginTime).le(Orders::getOrderTime, endTime);
//        }
        // 按订单时间降序排序
        queryWrapper.orderByDesc(ordersPage.getRecords() != null, Orders::getOrderTime);
        ordersService.page(ordersPage, queryWrapper);

        return Result.success(ordersPage);
    }

}
